package com.controller;


import com.bean.User;
import com.common.R;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


//1.将该类转换成控制器类，并设置该类中的方法返回值为json数据
@Controller
//解决跨域问题
@CrossOrigin
//@RestController相当于 Controller和ResponseBody的结合体
@ResponseBody
//该模块的请求路径为/user
@RequestMapping("/user")
public class UserController {
//    可以将其看做是web层
    @Autowired
    private UserService userService;

    /**
     * 根据用户id注册用户信息
     * @param userId
     * @return
     */

    @PostMapping("/getUserById")
    private R getUserById(String userId){
        try {
            User user = userService.getUserById(userId);
            if (user!=null){
                System.out.println("用户信息已经存在");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return R.ok();
    }

    @PostMapping("/saveUser")
    private R saveUser(User user){
        try {
            int i = userService.saveUser(user);
            if (i>0){
                return R.ok().msg("用户信息保存成功");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return R.error().msg("用户信息保存失败");
    }

    @PostMapping("/getUserByIdByPass")
//    将其封装为user对象，以user.getxxx()的形式获取需要的id，password
//    另一种形式   private R getUserByIdByPass(String userId,String password)
    private R getUserByIdByPass(User user){
        try {
            User loginUser = userService.getUserByIdByPass(user.getUserId(), user.getPassword());
            if (loginUser!=null){
                return R.ok().msg("loginUser");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
            return R.ok().msg("用户或密码错误");
    }

}
